package com.treetao.union.db.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.treetao.union.core.exception.DevMException;
import com.treetao.union.core.id.SnowflakeIdGenerator;
import com.treetao.union.db.mapper.MallCouponMapper;
import com.treetao.union.db.model.MallCoupon;
import com.treetao.union.db.model.MallCouponRecord;
import com.treetao.union.db.service.MallCouponRecordService;
import com.treetao.union.db.service.MallCouponService;

/**
 * <p>
 * 优惠券 服务实现类
 * </p>
 *
 * @author Dev.M
 * @since 2020-05-22
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class MallCouponServiceImpl extends ServiceImpl<MallCouponMapper, MallCoupon> implements MallCouponService {

	@Autowired
	private MallCouponRecordService couponRecordService;
	
	@Override
	public List<MallCoupon> getCanGetAvaliableCoupons() {
		Date cur = new Date();
		QueryWrapper<MallCoupon> q = new QueryWrapper<MallCoupon>();
		q.eq("del_flag", false);
		//在有效领取时间内
		q.le("startTime", cur);
		q.ge("endTime", cur);
		q.select("id","price","num","name","invalidEndTime","man");
		return list(q);
	}

	@Override
	public List<MallCouponRecord> getMyCanUseCoupons(Long userId,Double max) {
		return couponRecordService.getAvaliableCouponRecord(userId,max);
	}

	@Override
	public List<MallCouponRecord> getMyUsedCoupons(Long userId) {
		return couponRecordService.getUsedCouponRecord(userId);
	}

	@Override
	public List<MallCouponRecord> getMyOverdateCoupons(Long userId) {
		return couponRecordService.getOverdateCouponRecord(userId);
	}

	@Transactional
	@Override
	public boolean receiveCoupon(Long userId, Long couponId) {
		if(couponRecordService.isReceivedCoupon(userId, couponId)) {
			throw new DevMException("已领取过该券");
		}
		Date cur = new Date();

		MallCoupon coupon = getById(couponId);
		if(coupon==null) {
			throw new DevMException("无效的券");
		}
		
		if(cur.getTime() < coupon.getStartTime().getTime()) {
			throw new DevMException("未到领取时间");
		}
		if(cur.getTime() > coupon.getEndTime().getTime()) {
			throw new DevMException("已超过领取时间");
		}
		int receivedNum = coupon.getReceivedNum() == null ? 0 : coupon.getReceivedNum().intValue();
//		int count = couponRecordService.getReceivedNumt(couponId);
		if(receivedNum >= coupon.getNum().intValue()) {
			throw new DevMException("该券已领完了");
		}
		
		MallCouponRecord cp = new MallCouponRecord();
		cp.setCouponId(couponId);
		cp.setCouponSno(SnowflakeIdGenerator.nextId());
		cp.setGetTime(cur);
		cp.setInvalidEndTime(coupon.getInvalidEndTime());
		cp.setMan(coupon.getMan());
		cp.setPrice(coupon.getPrice());
		cp.setUsed(false);
		cp.setUserId(userId);
		//保存领取记录
		boolean flag = couponRecordService.save(cp);
		if(flag) {
			//领取数量+1
			coupon.setReceivedNum(receivedNum + 1);
			flag = updateById(coupon);
		}
		
		return flag;
	}

	@Override
	public boolean isReceivedCoupon(Long userId, Long couponId) {
		return couponRecordService.isReceivedCoupon(userId, couponId);
	}

	@Override
	public MallCouponRecord getMyCanUseCoupon(Long userId, Long couponId) {
		MallCouponRecord mr = couponRecordService.getMyCoupon(userId, couponId);
		if(mr==null) {
			throw new DevMException("券不存在");
		}
		if(mr.getUsed()) {
			throw new DevMException("该券已被使用");
		}
		if(System.currentTimeMillis() > mr.getInvalidEndTime().getTime()) {
			throw new DevMException("该券已过期");
		}
		return mr;
	}

	@Override
	public boolean setCouponUsed(Long userId,Long couponId) {
		return couponRecordService.setCouponUsed(userId, couponId);
	}

}
